package org.darcy.config;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.darcy.framework.interceptor.BraumInterceptor;
import org.darcy.framework.interceptor.RememberAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * web.xml
 */
@Configuration
@Slf4j
public class WebMvcConfig extends WebMvcConfigurationSupport {

	@Value("${app.localPath}")
	private String localPath;

	@Autowired
	private BraumInterceptor BraumInterceptor;

	@Autowired
	private RememberAuthenticationInterceptor rememberAuthenticationInterceptor;

	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		log.info("配置 静态资源访问");
		registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/")
				.setCachePeriod(3600);
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/localfile/**").addResourceLocations("file:" + localPath);
		super.addResourceHandlers(registry);
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(rememberAuthenticationInterceptor).excludePathPatterns("/admin/security/**",
				"/error/**", "/assets/**", "/localfile/**", "/getKaptcha/**", "/websocket")
				.addPathPatterns("/admin/**");

		registry.addInterceptor(BraumInterceptor).excludePathPatterns("/assets/**", "/localfile/**", "/error/**")
				.addPathPatterns("/**");
	}

	/**
	 * 添加自定义的Converters和Formatters.
	 * 
	 * 前台->后台
	 * 
	 * 支持多种日期形式
	 */
	@Override
	protected void addFormatters(FormatterRegistry registry) {
		registry.addConverter(new DateConverter());
	}

	// 定义时间格式转换器
	@Bean
	public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		converter.setObjectMapper(mapper);
		List<MediaType> list = new ArrayList<MediaType>();
		list.add(MediaType.APPLICATION_JSON);
		list.add(MediaType.TEXT_HTML);
		list.add(MediaType.TEXT_PLAIN);
		list.add(MediaType.APPLICATION_XML);
		list.add(MediaType.ALL);
		converter.setSupportedMediaTypes(list);
		return converter;
	}

	/**
	 * 添加自定义的Converters
	 * 
	 * 后台->前台
	 * 
	 * 日期返回的格式为: yyyy-MM-dd HH:mm:ss
	 */
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		log.info("配置 JSON转换器");
		converters.add(jackson2HttpMessageConverter());
		super.configureMessageConverters(converters);
	}

	@Bean
	public RequestContextListener requestContextListener() {
		return new RequestContextListener();
	}

}
